var poem_animal, poem_human, poem_quality, poem_object, poem_group;
var poem_action, poem_location, poem_situation;

loadpoem = function(lang){
	$("#poem-generator").each(function(){
		var submitText ="";
		lang=="fr" ? submitText="Générer le poème" : lang=="it" ? submitText="Generare la poesia" : false;
		
		$(this).append('<form id="generate-poem" method="get" action="">'+
							'<input name="generate" value="'+ submitText +'" onclick="" type="submit"/>'+
							'</form>');
							
		$("#generate-poem").submit(function(){
			getParts(lang);		
			fillPoem(lang);
			return false;
		});
		
	});
}

fillPoem = function(lang){
	if(lang=="fr"){
		$("span[name='poem_title']").html( filtertitle(poem_animal, lang) );
		$("span[name='poem_human']").html(poem_human);
		$("span[name='poem_quality']").html(articleToPronoun(poem_quality, lang));
		$("span[name='poem_animal']").html(poem_animal);
		$("span[name='poem_object']").html(poem_object);
		$("span[name='poem_group']").html(poem_group);
	}
	if(lang=="it"){
		$("span[name='poem_title']").html( capitalize(poem_action) + " in "+ poem_location );
		$("span[name='poem_action']").html(poem_action);
		$("span[name='poem_quality']").html(articleToPronoun(poem_quality, lang));
		$("span[name='poem_location']").html(poem_location);
		$("span[name='poem_situation']").html(poem_situation);
	}
	
	$("#poem-output").removeClass("invisible");
}

getParts = function(lang){
	if(lang=="fr"){
		$("#poem-input").find("#poem_animal").each(function(){poem_animal= this.value.toLowerCase();});
		$("#poem-input").find("#poem_human").each(function(){poem_human= this.value.toLowerCase();});
		$("#poem-input").find("#poem_quality").each(function(){poem_quality= this.value.toLowerCase();});
		$("#poem-input").find("#poem_object").each(function(){poem_object= this.value.toLowerCase();});
		$("#poem-input").find("#poem_group").each(function(){poem_group= this.value.toLowerCase();});
	}
	if(lang=="it"){
		$("#poem-input").find("#poem_action").each(function(){poem_action= this.value.toLowerCase();});
		$("#poem-input").find("#poem_location").each(function(){poem_location= this.value.toLowerCase();});
		$("#poem-input").find("#poem_quality").each(function(){poem_quality= this.value.toLowerCase();});
		$("#poem-input").find("#poem_situation").each(function(){poem_situation= this.value.toLowerCase();});
	}
	
}

capitalize = function(word){
	return word.substr(0,1).toUpperCase() + word.substr(1).toLowerCase();
}

articleToPronoun = function(expr, lang){
	var article;
	var word;
	
	if( expr.split(" ").length == 2){
		article = expr.split(" ")[0];
		word = expr.split(" ")[1];
	}
	else if( expr.split("'").length == 2){
		article = expr.split("'")[0];
		word = expr.split("'")[1];
	}
	else if( expr.split("`").length == 2){
		article = expr.split("`")[0];
		word = expr.split("`")[1];
	}
	
	switch(article){
		case "le" : {
			return "son " + word;
		}
		break;
		case "la" :{
			return "sa " + word;
		}
		break;
		case "l" :{
			return "son " + word;
		}
		break;
		default : {
			return expr
		}
	}	
}

filtertitle = function(expr, lang){
	var article;
	var resultPart;
	if( expr.split(" ").length == 2){
		article = expr.split(" ")[0];
		resultPart = capitalize(expr.split(" ")[1]);
	}
	else if( expr.split("'").length == 2){
		article = expr.split("'")[0];
		resultPart = capitalize(expr.split("'")[1]);
	}
	else if( expr.split("`").length == 2){
		article = expr.split("`")[0];
		resultPart = capitalize(expr.split("`")[1]);
	}
	else return;
	switch(article){
		case "un" : {
			return "Le " + resultPart;
		}
		break;
		case "une" :{
			return "La " + resultPart;
		}
		break;
		default :{
			return expr;
		}
	}
}